# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from .settings import DATABASE
import pymysql
from scrapy.contrib.exporter import JsonItemExporter
import json


class BossPipeline(object):

    def process_item(self, item, spider):
        return item


# class MySQLPipeline(object):
#     def __init__(self):
#         """
#         连接数据库
#         """
#         self.conn = pymysql.connect(
#             host=DATABASE.get('host'),
#             port=DATABASE.get('port'),
#             user=DATABASE.get('user'),
#             password=DATABASE.get('password'),
#             db=DATABASE.get('db'),
#             charset=DATABASE.get('charset')
#         )
#         self.cursor=self.conn.cursor()
#
#     def process_item(self,item,spider):
#         """
#         把每条item插入数据库
#         执行sql语句
#         self.cursor.execute(sql)
#         """
#         # print(item.get('name'))
#
#         if item.get('name'):
#             # sql = 'insert into zhilian(name) values("%s")' % (item['name'])
#             # self.cursor.execute(sql)
#             # print(item.get('name'))
#             # 为防止sql注入攻击,换个写法
#             sql = 'insert into info(name,desc,baseinfo,space) values(%s,%s,%s,%s)'
#             self.cursor.execute(sql, (item['name'], item['desc'], item['baseinfo'], item['space']))
#             self.conn.commit()
#         return item
#
#     def close_spider(self, spider):
#         """
#         关闭spider时,关闭数据库连接
#         """
#         self.cursor.close()
#         self.conn.close()


class JsonPipeline(object):
    """
    使用自带方法
    """
    def __init__(self):
        self.f = open('job.json','wb')
        self.exporter = JsonItemExporter(self.f,encoding='utf-8')
        self.exporter.start_exporting()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item

    def close_spider(self, spider):
        self.exporter.finish_exporting()